package com.bill.app.serveur;


import java.io.*;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import com.bill.app.serveur.bean.Abonnes;
import com.bill.app.serveur.bean.dao.implement.MainDaoImplement;

 
class ThreadConnexion extends Thread
{

    ThreadConnexion(Socket paquet, String mess)
    {
        this.paquet = null;
        this.paquet = paquet;
        this.mess = mess;
        start();
    }

    public void run()
    {
        PrintWriter ecriture = null;
        try
        {
            ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(paquet.getOutputStream())), true);
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
        StringTokenizer t = new StringTokenizer(mess, ":");
        t.nextToken();
        String login = t.nextToken().trim();
        System.out.println(login);
        String pass = t.nextToken();
        System.out.println(pass);
        String temp;
        if(existDansAbonne(login.trim(), pass))
        {
            if(EtatCompte.compareToIgnoreCase(Abonnes.COMPTE_IN_ACTIVE) == 0)
                temp = "<html><font color=red>votre compte<br> est bloqu\351!!</font></html>";
            else
                temp = "ok";
        } else
        {
            temp = "<html>Login ou mot de pass<br> incorrect ou votre <br>compte est inexistant!!</html>";
        }
        try
        { 
            ecriture.println(temp);
        }
        catch(Throwable e)
        {
            e.printStackTrace();
        }
        Thread.yield();
        try
        {
            paquet.close();
            ecriture.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public static boolean existDansAbonne(String log, String pass_w)
    {  MainDaoImplement em = SERVEUR.getEm();
    	Abonnes ab = (em).loadAbonneByLogin(log);
    	if(ab==null)return false;
    	EtatCompte=ab.getEtatCompte();
    	 return  em.isSamePassword(ab, pass_w);
         
    }

    Socket paquet;
    String mess;
    static String EtatCompte = "UNLOCKED";

}
